//
//  FocusDoctorController.m
//  QuickCure1
//
//  Created by maoge on 16/10/30.
//  Copyright © 2016年 maoge. All rights reserved.
//

#import "FocusDoctorController.h"
#import "ChooseDoctorModel.h"
#import "ChooseDoctorCell.h"
#import <Masonry.h>
#import "DoctorInfoController.h"

@interface FocusDoctorController ()<UITableViewDelegate,UITableViewDataSource>
//记录网络数据
@property (nonatomic,strong)NSArray<ChooseDoctorModel *> *modelList7;
//记录网络数据
//@property (nonatomic,strong)NSArray<ChooseDoctorModel *> *modelList13;
//记录tableView
@property (nonatomic,weak)UITableView *tableView;

@end

static NSString *cellId = @"cellId";

@implementation FocusDoctorController

- (void)viewDidLoad {
    [super viewDidLoad];
    //获取网络数据
    [self loadData];
    //搭建界面
    [self setupUI];
    
}

#pragma mark
#pragma mark - 调用模型中的类方法获取网络数据
- (void)loadData{
    
    NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"doctorList.plist"];
    NSLog(@"%@",filePath);
    //从本地读取医生列表信息,如果本地能读取到,就直接属性赋值,并且加载,如果没有就网络加载
    NSArray *doctorArr = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
    if (doctorArr ) {
        //加载成功后属性赋值,内存存储列表,优化运行速度
        self.modelList7 = doctorArr;
        
    }else{
        [ChooseDoctorModel getModelList7WithComplete:^(NSArray<ChooseDoctorModel *> *modelList, NSError *error) {
            if (error) {
                NSLog(@"error = %@",error);
            }else{
                self.modelList7 =modelList.copy;
                [self.tableView reloadData];
                
                //将医生列表存储到本地
                [NSKeyedArchiver archiveRootObject:self.modelList7 toFile:filePath];
                NSLog(@"7号接口---------------------%@",self.modelList7);
            }
        }];
        
    }
}

#pragma mark
#pragma mark - 搭建界面
- (void)setupUI{
    
    UITableView *tableView = [[UITableView alloc]init];
    
    [self.view addSubview:tableView];
    self.tableView = tableView;
    
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    tableView.dataSource = self;
    tableView.delegate = self;
    
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView.rowHeight = 100;
    
    [tableView registerNib:[UINib nibWithNibName:@"ChooseDoctorCell" bundle:nil] forCellReuseIdentifier:cellId];
    
    
}


#pragma mark
#pragma mark - tableView 的数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.modelList7.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ChooseDoctorCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
    
    ChooseDoctorModel *model7 = self.modelList7[indexPath.row];
//    NSLog(@"%zd",indexPath.row);

    cell.model7 = model7;

    //设置分割线
    UIImageView *separatView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"table_item_bk_with_top_gray_corner"]];
    [cell.contentView addSubview:separatView];
    
    [separatView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.bottom.equalTo(cell.contentView);
        make.height.mas_equalTo(5);
    }];
    
    
    
    return cell;
}


#pragma mark
#pragma mark - tableView代理方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    DoctorInfoController *VC = [[DoctorInfoController alloc]init];
    
    ChooseDoctorModel *model = self.modelList7[indexPath.row];
    
    VC.model = model;
    
    VC.indexPath = indexPath;
    
    VC.backBlock = ^(){
        NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"doctorList.plist"];
        self.modelList7 =  [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
        [self.tableView reloadData];
    };
    
    [self.navigationController pushViewController:VC animated:YES];
}




- (void)dealloc{
    
    NSLog(@"选择医生页面销毁");
}















@end
